<?php

namespace Es\Struct\Fields;

use Es\Constant\FieldsConst;
use Es\Contract\FieldsInterface;
use Es\Exceptions\CommonExceptions;

class DateStruct implements FieldsInterface
{
    
    private $name;
    private $type;
    private $format;

    const TYPE_LIST = [
        FieldsConst::DATE      => 1,
        FieldsConst::DATE_NANO => 1,
    ];

    /**
     * @throws CommonExceptions
     */
    public function __construct(
        string $name, 
        string $type = FieldsConst::KEYWORD,
        string $format = 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis'
    ){
        $this->name    = $name;
        $this->type    = $type;
        $this->format  = $format;
        if(!isset(self::TYPE_LIST[$type])){
            throw new CommonExceptions(CommonExceptions::FILEDS_NUM_NOT_FOUNT);
        }
    }   
    public function asArray(): array
    {
        return [ 
            $this->name, 
            [
                'type'   => $this->type,
                'format' => $this->format
            ]
        ];
    }
}
